//
//  ViewController.h
//  外星球生活圈
//
//  Created by MR.ZHOU on 15/8/12.
//  Copyright (c) 2015年 MR.ZHOU. All rights reserved.
//
#import "ZFAdsTableViewCell.h"
#import "iCarousel.h"

@interface ZFAdsTableViewCell ()<iCarouselDataSource,iCarouselDelegate>



@property (weak, nonatomic) IBOutlet iCarousel *adsView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property(nonatomic,strong)NSTimer *timer;

@end

@implementation ZFAdsTableViewCell

- (void)awakeFromNib {
    // Initialization code
    
    //iCaroousel初始化
    _adsView.pagingEnabled = YES;
    _adsView.type = iCarouselTypeLinear;
    _adsView.delegate = self;
    _adsView.dataSource = self;
    
  
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(void)setList:(FocusListModel *)list{
    _list =list;
    _pageControl.numberOfPages = _list.list.count;
    
    _pageControl.currentPage = 0;
    
    FocusModel *model = _list.list[0];
    _titleLabel.text = model.title;
    
    [_adsView reloadData];
    
    [self timeStart];
}
-(void)timeStart{
    _timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(autoRun) userInfo:nil repeats:YES];
    
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
}


-(void)autoRun{
    [UIView animateWithDuration:1.0 animations:^{
        _adsView.currentItemIndex = (_adsView.currentItemIndex + 1)%_list.list.count;
    }];
}


- (void)carouselWillBeginDragging:(iCarousel *)carousel{
    if (_timer.valid) {
        [_timer invalidate];
        _timer = nil;
    }
}

- (void)carouselDidEndDragging:(iCarousel *)carousel willDecelerate:(BOOL)decelerate{
    if (_timer == nil) {
        [self timeStart];
    }
    
}




- (BOOL)shouldAutorotateToInterfaceOrientation:(__unused UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}








#pragma mark -
#pragma mark iCarousel methods

- (NSInteger)numberOfItemsInCarousel:(__unused iCarousel *)carousel
{
    return (NSInteger)[_list.list count];
}

- (UIView *)carousel:(__unused iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    
     FocusModel *model = _list.list[index];
    //create new view if no view is available for recycling
    if (view == nil)
    {
        view = [[UIImageView alloc] initWithFrame:self.bounds];
         [((UIImageView *)view) sd_setImageWithString:model.cover];;
       
    }
   
    
    //set item label
    //remember to always set any properties of your carousel item
    //views outside of the `if (view == nil) {...}` check otherwise
    //you'll get weird issues with carousel item content appearing
    //in the wrong place in the carousel
   
    
    return view;
}

- (NSInteger)numberOfPlaceholdersInCarousel:(__unused iCarousel *)carousel
{
    //note: placeholder views are only displayed on some carousels if wrapping is disabled
    return 2;
}

- (UIView *)carousel:(__unused iCarousel *)carousel placeholderViewAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    
    FocusModel *model = _list.list[index];
    //create new view if no view is available for recycling
    if (view == nil)
    {
        //don't do anything specific to the index within
        //this `if (view == nil) {...}` statement because the view will be
        //recycled and used with other index values later
        view = [[UIImageView alloc] initWithFrame:self.bounds];
//        ((UIImageView *)view).image = [UIImage imageNamed:@"1.jpg"];
        [((UIImageView *)view) sd_setImageWithString:model.cover];
        //        view.contentMode = UIViewContentModeCenter;
          }
    else
    {
        //get a reference to the label in the recycled view
      
    }
    
    
    return view;
}

- (CATransform3D)carousel:(__unused iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
    //implement 'flip3D' style carousel
    transform = CATransform3DRotate(transform, M_PI / 8.0f, 0.0f, 1.0f, 0.0f);
    return CATransform3DTranslate(transform, 0.0f, 0.0f, offset * self.adsView.itemWidth);
}

- (CGFloat)carousel:(__unused iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    //customize carousel display
    switch (option)
    {
        case iCarouselOptionWrap:
        {
            //normally you would hard-code this to YES or NO
            return YES;
        }
        case iCarouselOptionSpacing:
        {
            //add a bit of spacing between the item views
            return value * 1.05f;
        }
        case iCarouselOptionFadeMax:
        {
            if (self.adsView.type == iCarouselTypeCustom)
            {
                //set opacity based on distance from camera
                return 0.0f;
            }
            return value;
        }
        case iCarouselOptionShowBackfaces:
        case iCarouselOptionRadius:
        case iCarouselOptionAngle:
        case iCarouselOptionArc:
        case iCarouselOptionTilt:
        case iCarouselOptionCount:
        case iCarouselOptionFadeMin:
        case iCarouselOptionFadeMinAlpha:
        case iCarouselOptionFadeRange:
        case iCarouselOptionOffsetMultiplier:
        case iCarouselOptionVisibleItems:
        {
            return value;
        }
    }
}

#pragma mark -
#pragma mark iCarousel taps

- (void)carousel:(__unused iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    NSNumber *item = (self.list.list)[(NSUInteger)index];
 
}

- (void)carouselCurrentItemIndexDidChange:(__unused iCarousel *)carousel
{

    FocusModel *model = _list.list[self.adsView.currentItemIndex];
    _pageControl.currentPage = self.adsView.currentItemIndex;
    
    _titleLabel.text = model.title;
}

@end
